package com.atguigu.yygh.user.controller.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.util.AuthContextHolder;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @FileName: PatientApiController
 * @Author Steven
 * @Date: 2021/8/11
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    PatientService patientService;

    /**
     * 查询当前登录就诊人信息列表
     * @param request
     * @return 就诊人列表
     */
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList = patientService.getAllPatient(userId);
        return Result.ok(patientList);
    }

    /**
     * 根据ID获取当前病人信息
     * @param id 用户编号
     * @return 病人详细信息
     */
    @GetMapping("auth/get/{id}")
    public Patient getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return patient;
    }

    /**
     * 保存当前就诊人信息
     * @param patient 病人信息
     * @param request 获取请求中用户信息
     * @return 病人信息
     */
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    /**
     * 修改就诊人信息
     * @param patient
     * @return
     */
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        boolean result = patientService.updateById(patient);
        return Result.ok(result);
    }

    /**
     * 根据ID删除就诊人信息
     * @param id
     * @return
     */
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id){
        boolean flag = patientService.removeById(id);
        return Result.ok(flag);
    }

    @ApiOperation("获取病人订单信息")
    @GetMapping("inner/get/{id}")
    public Result getPatientOrder(@PathVariable("id") Long id){

        return Result.ok(patientService.getById(id));
    }

}
